﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.DigitalOutput;
using MINERP.BLL.LeaderOperate;

namespace MINERP.DigitalOutput
{
    public partial class FrmAutoAssion : Form
    {
        public FrmAutoAssion()
        {
            InitializeComponent();
        }

        public FrmAutoAssion(int inTechid,int inUserid)
        {
            InitializeComponent();
            Techid = inTechid;
            userid = inUserid;
        }
     

       
        private bool isStart = false;
        BLL.DigitalOutput.DigitalOutputBLL DBLL = new MINERP.BLL.DigitalOutput.DigitalOutputBLL();
        private int AotuCount = 0;
        DataTable ErrTable=new DataTable();
        private int Techid;
        private int userid;
        private void FrmAutoAssion_Load(object sender, EventArgs e)
        {
            this.textEditTimeSpan.Text = Properties.Settings.Default.AutoAssionTaskTimeSpan;
            if (isStart)
            {
                this.simpleButtonOK.Text = "关闭自动分配";
            }
            else
            {
                this.simpleButtonOK.Text = "开启自动分配";
            }
            progressBarControl1.Visible = false;
            timerPross.Enabled = false;
            DBLL.DistributePrinter += new DigitalOutputBLL.ShowMessage(DBLL_DistributePrinter);
        }
        delegate void UpLeableText(DataTable msg, int count);
        void DBLL_DistributePrinter(DataTable table, int disCount)
        {
            if (labelControl4.InvokeRequired)
            {
                this.Invoke(new UpLeableText(DBLL_DistributePrinter), new object[] { table, disCount });
                return;
            }
            else
            {
                AotuCount += disCount;
                labelControl4.Text = "累计自动分配照片数：" + AotuCount + "出错数：" + table.Rows.Count;
                ErrTable = table;
            }
        }
         
        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
          
            Properties.Settings.Default.AutoAssionTaskTimeSpan = textEditTimeSpan.Text;
           
            Properties.Settings.Default.Save();
            //if (isStart)
            //{
            //    this.simpleButtonOK.Text = "关闭自动分配";
            //    //关闭
            //    EndtTimer(checkEditISCallBack.Checked);
            //    labelControl_Num.Visible = true;
            //    progressBarControl1.Visible = true;
            //    //progressBarControl1.Properties;
            //    //this.Close();
            //}
            //else
            //{
            //    this.simpleButtonOK.Text = "开启自动分配";
            //    //开启
            //    labelControl_Num.Visible = false;
            //    progressBarControl1.Visible = false;
            //    StartTimer(checkEditISCallBack.Checked);
            //    //this.Close();
            //}
            if (simpleButtonOK.Text.Equals("开启自动分配"))
            {
                textEditTimeSpan.Enabled = false;
                int timeSpan = Convert.ToInt32(Convert.ToDecimal(textEditTimeSpan.Text)*60*1000);
                DBLL.StartAutoDistribute(Techid, userid, timeSpan);
                progressBarControl1.Visible = true;
                simpleButtonOK.Text = "关闭自动分配";
               
                timerPross.Enabled = true;
            }
            else
            {
                textEditTimeSpan.Enabled = true;

                DBLL.EndAutoDistribute();
                progressBarControl1.Visible = false;
                simpleButtonOK.Text = "开启自动分配";
               
                timerPross.Enabled = false;
            }
        }

        private void simpleButtonCancel_Click(object sender, EventArgs e)
        {
            if (System.Windows.Forms.DialogResult.OK == MessageBox.Show("关闭窗口会关闭自动分配任务，确定关闭吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                this.Close();

            }
        }

        private void FrmAutoAssion_FormClosing(object sender, FormClosingEventArgs e)
        {
          
            //if (System.Windows.Forms.DialogResult.OK == MessageBox.Show("关闭窗口会关闭自动分配任务，确定关闭吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            //{
            DBLL.EndAutoDistribute();

            //}
            //else
            //{
              
            //}
        }

        private int proPosion = 0;
        private void timerPross_Tick(object sender, EventArgs e)
        {
            proPosion = proPosion%100;
            proPosion = proPosion + 1;
            progressBarControl1.Position = proPosion;
        }

        private void labelControl_Num_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            FrmAutoDistriErr frmAutoDistriErr = new FrmAutoDistriErr(ErrTable);
            frmAutoDistriErr.Show();
        }

        private void tsBackForm_Click(object sender, EventArgs e)
        {
          
            this.Visible = true;

        }
    }
}
